#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NS 15
#define PN 30
#define CN 6
typedef struct student
{
    int number;
    char name[NS];
    int course[CN];
    int sum;
}STUDENT;
void Initial(STUDENT stu[PN])
{
    int i;
    for(i=0;i<PN;i++)
    {
        stu[i].sum=-1;
    }
}
void Input(STUDENT stu[PN],int *pp,int *pc)
{
    int i,j;
    printf("Please input how many students:");
    scanf("%d",pp);
    printf("Please input how many course:");
    scanf("%d",pc);
    for(i=0;i<*pp;i++)
    {
        printf("Please input the No.%d student's ID number:\n",i+1);
        scanf("%d",&stu[i].number);
        getchar();
        printf("Please input the No.%d student's name:\n",i+1);
        gets(stu[i].name);
        for(j=0;j<*pc;j++)
        {
            printf("Please input the No.%d student's score of No.%d course:",i+1,j+1);
            scanf("%d",&stu[i].course[j]);
        }
    }
}
void ToAveCour(STUDENT stu[PN],int p,int c,int csum[CN],double cave[CN])
{
    int i,j;
    for(i=0;i<c;i++)
    {
        for(j=0;j<p;j++)
        {
            csum[i]+=stu[j].course[i];
        }
        cave[i]=(double)csum[i]/p;
    }
}
void OutToAveCour(STUDENT stu[PN],int c,int csum[CN],double cave[CN])
{
    int i;
    for(i=0;i<c;i++)
    {
        printf("The total score of No.%d course is %d\n",i+1,csum[i]);
        printf("The average score of No.%d course is %lf\n",i+1,cave[i]);
    }
}
void ToAveStu(STUDENT stu[PN],int p,int c,int ssum[PN],double save[PN])
{
    int i,j;
    for(i=0;i<p;i++)
    {
        for(j=0;j<c;j++)
        {
            ssum[i]+=stu[i].course[j];
        }
        save[i]=(double)ssum[i]/c;
        stu[i].sum=ssum[i];
    }
}
void OutToAveStu(STUDENT stu[PN],int p,double save[PN])
{
    int i;
    for(i=0;i<p;i++)
    {
        printf("The total score of No.%d student is %d\n",i+1,stu[i].sum);
        printf("The average score of No.%d student is %lf\n",i+1,save[i]);
    }
}
void DeRank(STUDENT stu[PN],int p)
{
    int i,j;
    STUDENT temp;
    for(i=0;i<p;i++)
    {
        for(j=i+1;j<p;j++)
        {
            if(stu[i].sum<stu[j].sum)
            {
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
        }
    }
}
void OutDeRank(STUDENT stu[PN],int p)
{
    int i;
    printf("According to total score(Descending),the rank is:\n");
    for(i=0;i<p;i++)
    {
        printf("No.%02d:",i+1);
        puts(stu[i].name);
        printf("      Total score:%d\n",stu[i].sum);
    }
}
void AsRank(STUDENT stu[PN],int p)
{
    int i,j;
    STUDENT temp;
    for(i=0;i<p;i++)
    {
        for(j=i+1;j<p;j++)
        {
            if(stu[i].sum>stu[j].sum)
            {
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
        }
    }
    printf("According to total score(Ascending),the rank is:\n");
    for(i=0;i<p;i++)
    {
        printf("No.%02d:",p-i);
        puts(stu[i].name);
        printf("      Total score:%d\n",stu[i].sum);
    }
}
void AsIDRank(STUDENT stu[PN],int p)
{
    int i,j;
    STUDENT temp;
    for(i=0;i<p;i++)
    {
        for(j=i+1;j<p;j++)
        {
            if(stu[i].number>stu[j].number)
            {
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
        }
    }
    printf("According to ID number(Ascending),the score chart:\n");
    for(i=0;i<p;i++)
    {
        printf("ID:%d  ",stu[i].number);
        puts(stu[i].name);
        printf("Total score:%d\n",stu[i].sum);
    }
}
void NameRank(STUDENT stu[PN],int p)
{
    int i,j;
    STUDENT temp;
    for(i=0;i<p;i++)
    {
        for(j=i+1;j<p;j++)
        {
            if(strcmp(stu[i].name,stu[j].name)>0)
            {
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
        }
    }
    printf("According to name order,the score chart:\n");
    for(i=0;i<p;i++)
    {
        printf("%s",stu[i].name);
        printf("   Total score:%d\n",stu[i].sum);
    }
}
void IDFind(STUDENT stu[PN],int p)
{
    int i,counter=0;
    int ID;
    printf("Please input the ID number of the student you are looking for:\n");
    scanf("%d",&ID);
    for(i=0;i<p;i++)
    {
        if(ID==stu[i].number)
        {
            printf("The student's name:");
            puts(stu[i].name);
            printf("The student got No.%d grade,and the total score is:%d\n",i+1,stu[i].sum);
            counter++;
        }
    }
    if(0==counter)
    {
        printf("Failed to seek out!The student you're looking for doesn't even exist!\n");
    }
}
void NameFind(STUDENT stu[PN],int p)
{
    getchar();
    int i,counter=0;
    char name[NS];
    printf("Please input the name of the student you are looking for:\n");
    gets(name);
    for(i=0;i<p;i++)
    {
        if(0==strcmp(stu[i].name,name))
        {
            printf("%s ",stu[i].name);
            printf("got No.%d grade,and the total score is:%d\n",i+1,stu[i].sum);
            counter++;
        }
    }
    if(0==counter)
    {
        printf("Failed to seek out!The student you're looking for doesn't even exist!\n");
    }
}
void LevelRank(STUDENT stu[PN],int p,int c)
{
    int nice[CN]={0};
    int good[CN]={0};
    int middle[CN]={0};
    int pass[CN]={0};
    int fail[CN]={0};
    int i,j,level;
    for(i=0;i<c;i++)
    {
        for(j=0;j<p;j++)
        {
            level=stu[j].course[i]/10;
            if(level>=9)
                nice[i]++;
            else if(8==level)
                good[i]++;
            else if(7==level)
                middle[i]++;
            else if(6==level)
                pass[i]++;
            else if(level<=5)
                fail[i]++;
        }
    }
    for(i=0;i<c;i++)
    {
        printf("For No.%d course:\n",i+1);
        printf("%d students got \'nice\'.\n",nice[i]);
        printf("%d students got \'good\'.\n",good[i]);
        printf("%d students got \'middle\'.\n",middle[i]);
        printf("%d students got \'pass\'.\n",pass[i]);
        printf("%d students got \'fail\'.\n",fail[i]);
    }
}
void ListAll(STUDENT stu[PN],int p,int c,int csum[CN],double cave[CN])
{
    int i,j;
    for(i=0;i<p;i++)
    {
        printf("ID:%d  name:",stu[i].number);
        puts(stu[i].name);
        for(j=0;j<c;j++)
        {
            printf("The score of No.%d course:%d\n",j+1,stu[i].course[j]);
        }
        putchar('\n');
    }
}
void main()
{
    int p=0,c=0;
    STUDENT stu[PN];
    Initial(stu);
    int option,choice;
    int *pp=&p;
    int *pc=&c;
    int csum[CN]={0};
    double cave[CN];
    int ssum[PN]={0};
    double save[PN];
    MENU:
    printf("1.Input record\n");
    printf("2.Calculate total and average score of every course\n");
    printf("3.Calculate total and average score of every student\n");
    printf("4.Sort in descending order by total score of every student\n");
    printf("5.Sort in ascending order by total score of every student\n");
    printf("6.Sort in ascending order by number\n");
    printf("7.Sort in dictionary order by name\n");
    printf("8.Search by number\n");
    printf("9.Search by name\n");
    printf("10.Statistic analysis for every course\n");
    printf("11.List record\n");
    printf("0.Exit\n");
    printf("Please enter your choice:\n");
    scanf("%d",&option);
    putchar('\n');
    switch(option)
    {
        case 1:
        {
            Input(stu,pp,pc);
            break;
        }
        case 2:
        {
            if(0==csum[0])
            {
                ToAveCour(stu,p,c,csum,cave);
            }
            OutToAveCour(stu,c,csum,cave);
            break;
        }
        case 3:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
                OutToAveStu(stu,p,save);
            }
            else
                OutToAveStu(stu,p,save);
            break;
        }
        case 4:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
            }
            DeRank(stu,p);
            OutDeRank(stu,p);
            break;
        }
        case 5:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
            }
            AsRank(stu,p);
            break;
        }
        case 6:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
            }
            AsIDRank(stu,p);
            break;
        }
        case 7:
        {
            NameRank(stu,p);
            break;
        }
        case 8:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
            }
            DeRank(stu,p);
            IDFind(stu,p);
            break;
        }
        case 9:
        {
            if(-1==stu[0].sum)
            {
                ToAveStu(stu,p,c,ssum,save);
            }
            DeRank(stu,p);
            NameFind(stu,p);
            break;
        }
        case 10:
        {
            LevelRank(stu,p,c);
            break;
        }
        case 11:
        {
            if(0==csum[0])
            {
                ToAveCour(stu,p,c,csum,cave);
            }
            ListAll(stu,p,c,csum,cave);
            OutToAveCour(stu,c,csum,cave);
            break;
        }
        case 0:
        {
            exit(0);
            break;
        }
        default:
        {
            printf("Incorrectly input!Please input the order number.\n");
            break;
        }
    }
    putchar('\n');
    AGAIN:
    printf("Do you want to back to the start menu?\n");
    printf("1.Yes\n");
    printf("2.No\n");
    printf("Please enter your choice:\n");
    scanf("%d",&choice);
    putchar('\n');
    if(1==choice)
    {
        goto MENU;
    }
    else if(2==choice)
    {
        printf("Dropped out safely!\n");
        exit(0);
    }
    else
    {
        printf("Incorrectly input!Please input the order number.\n");
        putchar('\n');
        printf("Another change to choose again:\n");
        goto AGAIN;
    }
}
